import parseTemplateToToken from './parseTemplateToToken';
import renderTemplate from './renderTemplate';

export default class TemplateEngine {

    constructor() {
        this.domStr = '';
    }

    render(templateStr, data) {
        const token = parseTemplateToToken(templateStr);
        let domStr = renderTemplate(token, data);

        domStr = compress(domStr);
        this.domStr = domStr;

        return this;
    }

    $mounted(id) {
        const dom = document.querySelector(id);

        dom.innerHTML = this.domStr;
    }
}

function compress(str) {
    return str.replace(/>(\s+.*\s+) | (\s+.*) | (.*\s+)</g, (findStr) => {
        return findStr.replace(/\s/g, '');
    })
}